#include "../include/threadPool.h"
#include "../../command/command.h"
#include "../../command/metaFile.h"
void *threadFunc(void *arg){
    threadPool_t *pthreadPool = (threadPool_t *)arg;
    int netfd;
    while(1){
BEGIN:
        pthread_mutex_lock(&pthreadPool->mutex);
        while(pthreadPool->taskQueue.queueSize == 0){
            pthread_cond_wait(&pthreadPool->cond,&pthreadPool->mutex);
        }
        netfd = pthreadPool->taskQueue.front->data;
        printf("one worker get netfd = %d\n",netfd);
        deQueue(&pthreadPool->taskQueue);
        MYSQL *mysql;
        mysqlConnect(&mysql);
        pthread_mutex_unlock(&pthreadPool->mutex);// 得到netfd开始处理任务
        char msg1[1024] = "请输入用户名:";
        char msg2[1024] = "请输入密码:";
        char userName[30] = {0};
        char userPwd[30] = {0};
        char msg3[1024] = "密码错误，登录失败!\n";
        char msg4[1024] = "密码正确，登陆成功!\n";
        int regret,rret,uret,lret,pathId = -1;
        send(netfd,msg1,strlen(msg1),MSG_NOSIGNAL);
        rret = recv(netfd,userName,sizeof(userName),0);
        if(rret == 0){
            printf("对方关闭连接!\n");
            close(netfd);
            continue;
        }
        send(netfd,msg2,strlen(msg2),MSG_NOSIGNAL);
        rret = recv(netfd,userPwd,sizeof(userPwd),0);
        if(rret == 0){
            printf("对方关闭连接!\n");
            close(netfd);
            continue;
        }
        uret = isInTable(mysql,userName);
        if(uret == -1){
            regret = Register(mysql,userName,userPwd);
            if(regret == -1){
                close(netfd);
                continue;
            }
        }
        lret = logIn(mysql,userName,userPwd);
        if(lret == 0){
            pathId = getUserRootPathIdByUsername(mysql,userName);
            send(netfd,msg4,strlen(msg4),MSG_NOSIGNAL);
            char phyPath[1024] = "/home/playba/gitLibrary/test_net-disk3/net-disk3/server/src/FILE/";
            char buf[4096] = {0};
            char result[4096] = {0};
            char hashName[4096] = {0};
            int cmd,cret;
            while(1){
                bzero(buf,sizeof(buf));
                bzero(result,sizeof(result));
                bzero(hashName,sizeof(hashName));
                bzero(&cmd,sizeof(cmd));
                char *filePath = (char *)calloc(8192,sizeof(char));
                strcpy(filePath,phyPath);
                recvCmd(netfd,buf,&cmd);
                switch(cmd){
                case 0:
                    close(netfd);
                    goto BEGIN;
                case CD:
                    my_cd(mysql,buf,&pathId,result);
                    sendCmd(netfd,result,CD);
                    break;
                case LS:
                    printf("ls\n");
                    my_ls(mysql,buf,pathId,result);
                    sendCmd(netfd,result,LS); 
                    break;
                case PWD:
                    my_pwd(mysql,pathId,result);
                    sendCmd(netfd,result,PWD);
                    break;
                case PUTS:
                    recvCmd(netfd,hashName,&cmd);
                    if(cmd == -1){
                        printf("对方打开文件失败!\n");
                        break;
                    }
                    cret = checkHashNameExists(mysql,hashName);
                    if(cret == 0){// 极速秒传
                        sendCmd(netfd,hashName,-1);// 给用户返回上传成功
                        //fileMeta_t meta;// 增添当前用户相同文件的表项
                        //bzero(&meta,sizeof(meta));
                        //strcpy(meta.fileName,buf);
                        //strcpy(meta.filePath,filePath);
                        //strcpy(meta.userName,userName);
                        //strcpy(meta.hashName,hashName);
                        //meta.fileType = 'f';
                        //meta.parentId = pathId;
                        //insertDataToFileTable(mysql,&meta);
                        my_puts(mysql,buf,pathId,hashName);
                        printf("极速秒传成功!\n");
                        free(filePath);
                        break;
                    }
                    sendCmd(netfd,hashName,PUTS);
                    strcat(filePath,hashName);
                    recvFile(netfd,filePath,&cmd);
                    my_puts(mysql,buf,pathId,hashName);
                    free(filePath);
                    break;
                case GETS:
                    my_gets(mysql,buf,pathId,hashName,phyPath);
                    sendCmd(netfd,"1",GETS);
                    sendFile(netfd,hashName,buf,GETS);
                    break;
                case MKDIR:
                    my_mkdir(mysql,buf,pathId,result);
                    sendCmd(netfd,result,MKDIR);
                    break;
                case RMDIR:
                    removeDir(mysql,buf,pathId,result);
                    sendCmd(netfd,result,RMDIR);
                    break;
                case REMOVE:
                    removeFile(mysql,buf,pathId,result,phyPath);
                    sendCmd(netfd,result,REMOVE);
                    break;
                }
            }
        }else{
            send(netfd,msg3,strlen(msg3),MSG_NOSIGNAL);
            close(netfd);
        }
    }
    return NULL;
}

int makeWorker(threadPool_t *pthreadPool){
    for(int i = 0; i < pthreadPool->workerArr.workerNum; i++){
        pthread_create(&pthreadPool->workerArr.tidArr[i],NULL,threadFunc,pthreadPool);
        printf("worker %d\n",i);
    }
    return 0;
}
